package com.alison.SmartCli;
import com.alison.SmartCli.Utils.ReflectionUtil;
import com.alison.SmartCli.Utils.SimpleLogger;

import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Bootstrap {

    private static Set<Class> classSet = new HashSet<>();

    public static void bootstrap(){
        List<Class> cliClasses = ReflectionUtil.getClassesWithAnnotation(Cli.class);
        //扫描COMMAND
        for (Class clazz : cliClasses){
            handleClass(clazz);
        }
    }

    public static void handleClass(Class clazz){
        if (classSet.contains(clazz)){
            return;
        }
        Method[] methods = clazz.getMethods();
        for (Method method:methods){
            if(method.isAnnotationPresent(Command.class)){
                SimpleLogger.debug("扫描到"+method.getName());
                MethodData.addMethodClass(method.getName(),clazz);
                MethodData.addMethod(method.getName(),method);
                if(method.isAnnotationPresent(Usage.class)){
                    MethodData.addUsage(method.getName(),method.getAnnotation(Usage.class).value());
                }
            }
        }
        classSet.add(clazz);
    }

    public static void handlePackage(String packageName){
        List<Class> classes = ReflectionUtil.getClassesWithAnnotation(Cli.class,packageName);
        for (Class clazz:classes){
            handleClass(clazz);
        }
    }

}
